%%% web_helper.erl - helpers for dealing with xml over http web services
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2008 hellomatty@gmail.com
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%

-module(web_helper).

-export([apply_xpath/2, parse_xml_uri/1, get_uri/1, parse_xml/1, collect_text/1,value/1]).

-import(xmerl_scan).
-import(xmerl_xpath).
-import(html_entity_refs).
-import(http).
-include_lib("util.hrl").

-define(TIMEOUT, 10000).

apply_xpath(XPath, ParsedXml) ->
  xmerl_xpath:string(XPath, ParsedXml).

get_uri(Uri) ->
  get_uri(Uri, 3).
get_uri(_Uri, 0) ->
    exit(get_failed);
get_uri(Uri, Tries) ->
  case do_get(Uri) of
    {ok, Response} ->
      Response;
    _ -> 
      timer:sleep(5000),
%      ?D({web_helper,get,Uri,tries_left,Tries}),
      get_uri(Uri, Tries - 1)
  end.
do_get(Uri) ->
  case http:request(get, {Uri,[]}, [{timeout,?TIMEOUT}], []) of
    {ok, {_Status, _Headers, Body}} ->
      {ok, Body};
    _ = Error ->
%      ?D({web_helper,get,Uri,failed,Error}),
      {error, Error}
  end.

parse_xml_uri(Uri) ->
  try
    {ok, parse_xml( get_uri( Uri ) )}
  catch
    Tag:Exception ->
      ?D({web_helper,parse_xml_uri,failed,Uri}),
      exit({Tag, Exception})
  end.

parse_xml(RawXhtml) ->
  {ParsedXhtml, _} = xmerl_scan:string(RawXhtml, [{rules, html_entity_refs:get_xmerl_rules()}]),
  ParsedXhtml.

collect_text(TextNodes) ->
 lists:flatten([Text || {xmlText, _, _, _, Text, text} <- TextNodes]).

value([]) -> [];
value([{xmlAttribute, _Name, _, _, _, _, _, _, Value, _}]) ->
  Value.
